//
//  SelectAllViewController.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 light. All rights reserved.
//

#import "SelectAllViewController.h"
#import <AFNetworking.h>
#import "EveryDayModel.h"
#import "EveryStoryCollectionViewCell.h"
#import "EveryDayDetailViewController.h"
@interface SelectAllViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *allCollectionV;
@property (nonatomic, strong) NSMutableArray *dataArray;
//记录下一页
@property (nonatomic, assign) NSInteger nextPage;
//判断上拉下拉(yes是上拉)
@property (nonatomic, assign) BOOL isUpLoading;

@end

@implementation SelectAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"精选故事";
   //字体设为白色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName: [UIFont systemFontOfSize:22],NSFontAttributeName: [UIFont boldSystemFontOfSize:22]}];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:192 / 255.0 blue:203 / 255.0 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Cancel2"] imageWithRenderingMode:1] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30 ) / 2, 200 );
    flow.sectionInset = UIEdgeInsetsMake(10 , 10, 10 , 10 );
    flow.minimumInteritemSpacing = 10 ;
    flow.minimumLineSpacing = 10;
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.allCollectionV = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flow];
    self.allCollectionV.backgroundColor = [UIColor colorWithRed:251 / 255.0 green:248 / 255.0  blue:241 / 255.0 alpha:1];
    [self.view addSubview:self.allCollectionV];
    self.allCollectionV.delegate = self;
    self.allCollectionV.dataSource = self;
    [self.allCollectionV registerClass:[EveryStoryCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
   [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.isUpLoading = NO;
    [self addHeader];
    [self addFooter];
    
    
}

#pragma mark --下拉刷新
-(void)addHeader{
    //block中不能使用self,会报错
    __block SelectAllViewController *recommendVC = self;
    
    [self.allCollectionV addHeaderWithCallback:^{
        //记录页码
        recommendVC.nextPage = 1;
        //标记下拉刷新
        recommendVC.isUpLoading = NO;
        //请求数据
        [recommendVC getDataList:recommendVC.nextPage];
        
    }];
    
    
    //进入程序就开始下拉刷新
    [self.allCollectionV headerBeginRefreshing];
}

#pragma mark -- 上拉加载更多
-(void)addFooter{
    
    __block SelectAllViewController *recommendVC = self;
    //添加上拉刷新尾部控件
    [self.allCollectionV addFooterWithCallback:^{
        //上拉就加1
        recommendVC.nextPage += 12;
        
        //标记为上拉操作
        recommendVC.isUpLoading = YES;
        //请求数据
        [recommendVC getDataList:recommendVC.nextPage];
        
    }];
    
}

- (void)getDataList:(NSInteger)nextPage
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *string = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld",nextPage];
    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        NSArray *array = [dic objectForKey:@"hot_spot_list"];
        if (self.isUpLoading == NO) {
            [self.dataArray removeAllObjects];
        }
        for (NSDictionary *dic in array) {
            EveryDayModel *model = [[EveryDayModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            
        }
        [self.allCollectionV footerEndRefreshing];
        [self.allCollectionV headerEndRefreshing];
       [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.allCollectionV reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    EveryStoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    EveryDayModel *model = [self.dataArray objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    EveryDayDetailViewController *everVC = [[EveryDayDetailViewController alloc] init];
     EveryDayModel *model = [self.dataArray objectAtIndex:indexPath.row];
    everVC.spot_id = model.spot_id;
    everVC.type = @"1";
    
    
    
    [self.navigationController pushViewController:everVC animated:YES];
    
    
   
    
}


- (void)leftAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
